package com.itheima.service;

import com.itheima.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserApp {
    //入门案例
    @Test
    public  void test01() {
//    加载配置文件
        ApplicationContext ctx= new ClassPathXmlApplicationContext("ApplicationContext.xml");
//    获取资源
        UserService userService = (UserService)ctx.getBean("userService");
        userService.save();
    }

//scope属性
    @Test
    public  void test02() {
//    加载配置文件
        ApplicationContext ctx= new ClassPathXmlApplicationContext("ApplicationContext.xml");
//    创建对象
        UserService userService01 = (UserService)ctx.getBean("userService");
        UserService userService02 = (UserService)ctx.getBean("userService");
        UserService userService03 = (UserService)ctx.getBean("userService");
//   输出地址值
        System.out.println(userService01);
        System.out.println(userService02);
        System.out.println(userService03);
//   判断对象的地址值是否相同
        System.out.println(userService01==userService02);
//        userService01.save();
    }



//测试bean的生命周期
    @Test
    public  void test03() {
//    加载配置文件
       ClassPathXmlApplicationContext  ctx= new ClassPathXmlApplicationContext("ApplicationContext.xml");
//    获取资源
        UserService userService01 = (UserService)ctx.getBean("userService");
        UserService userService02 = (UserService)ctx.getBean("userService");
        UserService userService03= (UserService)ctx.getBean("userService");
//        userService.save();
        ctx.close();
//非单例不归spring Ioc容器管理
    }

    //静态工厂与实例工厂创建bean对象
    @Test
    public  void test04() {
//    加载配置文件
        ApplicationContext  ctx= new ClassPathXmlApplicationContext("ApplicationContext.xml");
//    获取资源
        UserService userService01 = (UserService)ctx.getBean("userServiceFactory01");
        UserService userService02 = (UserService)ctx.getBean("userServiceFactory02");
        userService01.save();
        userService02.save();

//非单例不归spring Ioc容器管理
    }
//set注入
    @Test
    public  void test05() {
//    加载配置文件
        ApplicationContext  ctx= new ClassPathXmlApplicationContext("ApplicationContext.xml");
//    获取资源
        UserService userService01 = (UserService)ctx.getBean("userService");
        userService01.save();
    }
}
